package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements IAttributeService {
    @Autowired
    private AttributeMapper attributeMapper;

    //检查：同一属性模板id下属性名称唯一
    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理添加属性的业务，参数：{}",attributeAddNewDTO);
        String name = attributeAddNewDTO.getName();
        List<AttributeListItemVO> list = attributeMapper.getListByTemplateId(attributeAddNewDTO.getTemplateId());
        if(list.size() > 0){
            for (AttributeListItemVO attributeListItemVO : list) {
                String itemVOName = attributeListItemVO.getName();
                log.debug("itemVOName={},name={}",itemVOName,name);
                if(name.equals(itemVOName)){
                    String message = "添加属性失败，属性名称已存在，请更换属性名称";
                    log.debug(message);
                    throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
                }
            }
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO,attribute);
        log.debug("即将添加属性");
        int rows = attributeMapper.insert(attribute);
        if(rows != 1){
            String message = "添加属性失败，服务器忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("添加属性完成");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除属性的业务，参数：{}",id);
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(id);
        if (attributeStandardVO == null) {
            String message = "删除属性失败，数据不存在，请更换其它的id";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int rows = attributeMapper.delete(id);
        if(rows != 1){
            String message = "删除属性失败，服务器忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("删除相册成功");
    }

    @Override
    public List<AttributeListItemVO> getListByTemplateId(Long templateId) {
        log.debug("开始处理根据【templateId】查询属性列表的业务，参数templateId：{}",templateId);
        return attributeMapper.getListByTemplateId(templateId);
    }
}
